<template>
  <!-- 修改资料 -->
  <div>
    <!-- 内容主题区域 -->
    <el-form
      label-width="120px"
      :model="form"
      label-position="right"
      ref="formRef"
      :rules="formRules"
    >
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="数据库表名" prop="number">
            <el-input
              style="width: 70%"
              v-model="form.number"
              placeholder="请输入数据库表名"
            ></el-input
            >&nbsp;
            <el-button style="width: 20%" type="primary" @click="search"
              >查询表字段</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="表描述" prop="name">
            <el-input v-model="form.name" placeholder="请输入表描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="映射实体名" prop="entityName">
            <el-input v-model="form.entityName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="表主键" prop="primaryFieldName">
            <el-input v-model="form.primaryFieldName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="记录唯一字段" prop="uniqueFields">
            <el-input v-model="form.uniqueFields" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="是否自增型主键" prop="sautoIncreasePrimary">
            <el-switch v-model="form.autoIncreasePrimary"> </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <sc-title title="表字段列表"></sc-title>
      <el-scrollbar height="400px">
        <sc-form-table
          v-model="dataList"
          :addTemplate="addTemplate"
          placeholder="暂无数据"
        >
          <template #tableContent>
            <el-table-column prop="number" label="字段名">
              <template #default="scope">
                <el-input
                  v-model="scope.row.number"
                  placeholder="请输入数据库表名"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="字段描述">
              <template #default="scope">
                <el-input
                  v-model="scope.row.name"
                  placeholder="请输入名称"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="fieldType" label="字段类型">
              <template #default="scope">
                <el-select
                  v-model="scope.row.fieldType"
                  clearable
                  placeholder="请选择："
                >
                  <el-option
                    v-for="item in configData.fieldTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="refMainTableName" label="关联主表名">
              <template #default="scope">
                <el-input v-model="scope.row.refMainTableName"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="refMainTableFieldName" label="关联主表字段">
              <template #default="scope">
                <el-input v-model="scope.row.refMainTableFieldName"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="lng" label="字段长度">
              <template #default="scope">
                <el-input v-model="scope.row.lng"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="scale" label="小数位数">
              <template #default="scope">
                <el-input v-model="scope.row.scale"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="nullable" label="是否可为空">
              <template #default="scope">
                <el-switch v-model="scope.row.nullable"> </el-switch>
              </template>
            </el-table-column>
            <el-table-column prop="autoIncrement" label="是否自增型">
              <template #default="scope">
                <el-switch v-model="scope.row.autoIncrement"> </el-switch>
              </template>
            </el-table-column>
            <el-table-column prop="autoIncrement" label="是否导入">
              <template #default="scope">
                <el-switch v-model="scope.row.importFlag" />
              </template>
            </el-table-column>
            
            <el-table-column prop="defaultValue" label="默认值">
              <template #default="scope">
                <el-input v-model="scope.row.defaultValue"></el-input>
              </template>
            </el-table-column>
          </template>
        </sc-form-table>
      </el-scrollbar>
    </el-form>
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="add(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close } from "@element-plus/icons-vue";
import Api from "@/api/model/meta";
const emit = defineEmits(["updatetable"]);
// 添加用户数据的对象
const form = ref({});
const configData = ref({});
const addTemplate = ref({});
const dataList = ref([]);
const formRef = ref();

onMounted(() => {
  getInit();
});

const getInit = async () => {
  let formRes = await Api.table.add.post();
  if (formRes.code == 200) {
    form.value = formRes.data;
    configData.value = formRes.configData;
    addTemplate.value = formRes.dtl;
  }
};

const search = async () => {
  if (form.value.number) {
    let param = {
      name: form.value.number,
    };
    let res = await Api.table.getTableSchema.post(param);
    dataList.value = res.dataList;
    form.value.primaryFieldName = res.primaryKey;
    form.value.entityName = res.entityName;
  } else {
    ElNotification.info({
      title: "",
      message: "请输入数据库表名！",
      duration: 2000,
    });
  }
};

const restAddFormData = () => {
  formRef.value.resetFields();
};

const updateVisible = (val) => {
  emit("updatetable");
};

// 上传
const add = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      addData();
    }
  });
};

const addData = async () => {
  let params = {
    bean: form.value,
    dataList: dataList.value,
  };
  let res = await Api.table.save.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "添加成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style lang="less" scoped>
</style>

